﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using TarreOA.TarreOADAL;
using TarreOA.TarreOAModels;

namespace TarreOA.TarreOABLL {
    public class Flow_WorkFeedBack_Service {
        /// <summary>
        /// 根据工作编号查找此工作的所有反馈信息
        /// </summary>
        /// <param name="work_id"></param>
        /// <returns></returns>
        public List<Flow_WorkFeedBack> FindAllByWorkId(int work_id) {
            List<Flow_WorkFeedBack> list = null;
            SqlConnection sqlConn = null;
            try {
                sqlConn = SQLManager.GetConn();
                string sql = "select * from Flow_WorkFeedBack where New_Work_Id = " + work_id;
                Flow_WorkFeedBack_DAO fwfb_dao = new Flow_WorkFeedBack_DAO();
                SqlDataReader sqlDr = fwfb_dao.FindByTest(sql, new Hashtable(), sqlConn);
                if(sqlDr != null && sqlDr.HasRows) {
                    list = new List<Flow_WorkFeedBack>();
                    while(sqlDr.Read()) {
                        Flow_WorkFeedBack fwfb = new Flow_WorkFeedBack();
                        fwfb.FeedBack_userName = (string) sqlDr["User_Lname"];
                        fwfb.FeedBack_content = (string) sqlDr["Feedback_Content"];
                        fwfb.FeedBack_time = (DateTime) sqlDr["Feedback_Time"];
                        list.Add(fwfb);
                    }
                }
            } catch(Exception ex) {
                Console.WriteLine(ex.Message);
            } finally {
                if(sqlConn != null && sqlConn.State == System.Data.ConnectionState.Open) {
                    sqlConn.Close();
                    sqlConn.Dispose();
                }
            }
            return list;
        }

        /// <summary>
        /// 保存用户的反馈信息
        /// </summary>
        /// <param name="work_id"></param>
        /// <param name="feed_content"></param>
        /// <param name="username"></param>
        /// <returns></returns>
        public int AddWorkFeedBack(int work_id, string feed_content, string username) {
            string sql = "insert into Flow_WorkFeedBack values(@work_id, @username, @feed_content, @feed_time)";
            Hashtable ht = new Hashtable();
            ht.Add("@work_id", work_id);
            ht.Add("@username", username);
            ht.Add("@feed_content", feed_content);
            ht.Add("@feed_time", DateTime.Now);
            Flow_WorkFeedBack_DAO fwfb_dao = new Flow_WorkFeedBack_DAO();
            return fwfb_dao.UpdateByTest(sql, ht);
        }
    }
}
